<!-- 公告管理 -->
<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="公告名称">
        <el-input v-model="searchMap.title" placeholder="请输入"></el-input>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker type="date" placeholder="选择日期" v-model="searchMap.created"></el-date-picker>
      </el-form-item>

      <el-form-item label="创建人">
        <el-select v-model="searchMap.userId" clearable placeholder="请选择">
          <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-button type="primary" @click="fetchData()">查询</el-button>
      <el-button type="primary" @click="handleEdit('')">新增</el-button>
    </el-form>

    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="序号"> </el-table-column>
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="content" label="内容"> </el-table-column>
      <el-table-column prop="scope" label="发布范围"></el-table-column>
      <el-table-column prop="userName" label="创建人"></el-table-column>
      <el-table-column prop="created" label="创建时间 " :formatter="dataFormat" width="160"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row.id)" type="text" size="small">修改</el-button>
          <el-button @click="handleDelete(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination @size-change="sizeData" @current-change="pageData" :page-sizes="[5, 10, 20, 30, 60]" :page-size="5"
      layout="total, sizes, prev, pager, next, jumper" :total="count">
    </el-pagination>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form label-width="80px">
        <el-row>
          <el-col span="11">
            <el-form-item label="标题"><el-input v-model="announceMannage.title" /></el-form-item>
          </el-col>
          <el-col span="11">
            <el-form-item label="内容"><el-input v-model="announceMannage.content" /></el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="11">
            <el-form-item label="发布范围">
              <el-select v-model="announceMannage.scope" clearable placeholder="请选择">
                <el-option v-for="item in fbLists" :key="item.value" :label="item.lable" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col span="11">
            <el-form-item label="创建人">
              <el-select v-model="announceMannage.userId">
                <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item>
          <el-button type="primary" @click="handleSave()" style="margin-left: 240px">保存</el-button>
          <el-button @click="dialogFormVisible = false" style="margin-left: 10px">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import announceMannagementService from "@/api/announceMannage";
//导入日期组件
import moment from 'moment'
import userService from "@/api/user";
export default {
  data() {
    return {
      tableData: [],
      list: [],
      userList:[],
      fbLists: [
        {
          lable: "全体成员",
          value: "全体成员"
        },
        {
          lable: "食堂成员",
          value: "食堂成员"
        },
        {
          lable: "员工部门",
          value: "员工部门"
        },
      ],
      dialogFormVisible: false,
      id: "",
      announceMannage: {},
      searchMap: {
        limit: 5,
        page: 1,
        title: "",
        created: "",
        userId: "",
      },
      //记录总数
      count: 0,
    };
  },
  //钩子函数
  created() {
    this.findAll();
    this.findAllUser();
    this.fetchData();
  },
  methods: {

    findAllUser(){
      userService.findAll().then(response=>{
        this.userList=response.data
      })
    },
    //修改或新增
    handleEdit(id) {

      this.id = id;

      //打开弹窗
      this.dialogFormVisible = true;

      if (id !== '') {

        //根据id查询数据绑定到模型上面
        announceMannagementService.find(id).then(response => {

          //回显
          this.announceMannage = response.data

          //回显图片
          // this.imageUrl = this.pojo.image

        })

      } else {

        //清除之前模型的脏数据
        this.announceMannage = {

        }

        this.imageUrl = ''
      }
    },

    //新增和修改的提交
    handleSave() {
      announceMannagementService.update(this.id, this.announceMannage).then((response) => {
        //新增或者修改成功
        this.$message({
          message: response.msg,
          type: "success",
        });

        //重新加载列表的数据
        this.fetchData();

        //关闭弹窗
        this.dialogFormVisible = false;
      });
    },

    //查询数据
    fetchData() {
      announceMannagementService.list(this.searchMap).then((response) => {
        this.tableData = response.data;
        this.count = response.count;
      });
    },
    //当页面大小改变的时候调用此方法，传递改变的页面大小
    sizeData(size) {
      this.searchMap.limit = size;
      this.fetchData();
    },
    pageData(page) {
      this.searchMap.page = page;
      this.fetchData();
    },


    //查询所有部门
    findAll() {
      announceMannagementService.findAll().then(response => {
        this.list = response.data
      })
    },

    //增加一个格式化日期的方法
    dataFormat(row, column) {

      let date = row[column.property]

      if (date == undefined) {
        return ''
      }

      return moment(date).format('YYYY-MM-DD hh:mm:ss')
    },


    //删除
    handleDelete(id) {
      this.$confirm('您确定要删除吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        announceMannagementService.remove(id).then(response => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });

          this.fetchData()

        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
};
</script>
